
import {LGXEnums}  from "../../../common/sysenum.js";

import utilfuncs from "../../../common/tools/utilfuncs.js";
var funcs = utilfuncs.UtilFuncs;
import { GlobalVar } from "../../../common/globalvar.js";
import plotsvcPkg from './plotservice.js'
var plotsvc = plotsvcPkg.PlotSvc;

import snapsvc from '../../../service/pointsnapsvc.js'
var ptSnapSvc = snapsvc.PointSnapSvc;

var LeftMoseUpPlotTool = {

}

LeftMoseUpPlotTool.action = function (dataManger, mosEvnt, mousePt) {
    console.log("lmosup-1");
    var pCurentGraph = dataManger.getCurentGraph();
    if (pCurentGraph){
        dataManger.recordRepaintScope(pCurentGraph);
    }
    if (pCurentGraph != null
        && pCurentGraph.getStart() == 1
        && pCurentGraph.getUseful() == 1
        && pCurentGraph.getFinish() == 0) {

        ptSnapSvc.snapMouse2OtherGraphPTFeature(dataManger, mousePt, false,true);
        pCurentGraph.act2Mouse(mosEvnt, mousePt);

        if (pCurentGraph.getFinish() == 1) {
            if (GlobalVar.editMode != "autocad" && funcs.group(pCurentGraph.getType()) != global.dimGroup) {
                if (pCurentGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type){
                    let linePos = pCurentGraph.getPosCopy();
                    let tailPt = linePos.getTail();
                    mousePt.x = tailPt.x;
                    mousePt.y = tailPt.y;
                }

                pCurentGraph.setFinish(0);//先暂时置为未绘制完成，以避免拾取到自己
                let scaling = dataManger.getScaling();
                var mayConnGraph = dataManger.pickGraph(mousePt, "");
                pCurentGraph.setFinish(1);//先暂时置为未绘制完成，以避免拾取到自己
                console.log(mayConnGraph);
                if (mayConnGraph && mayConnGraph != pCurentGraph) {
                    plotsvc.connect2DstGraph(dataManger.getTopoManager(), mousePt, pCurentGraph, mayConnGraph, false, scaling);
                    if (pCurentGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
                        // this.correctLineEndPt(pCurentGraph, scaling);
                        let lineDefInfo = pCurentGraph.getLineDef();
                        if (lineDefInfo) {
                            lineDefInfo.endArrowType = 'triangle_mid_fill_close';
                        }
                    }
                }
            }
            dataManger.m_pOperStepService.record(0, []);
        }

        if (pCurentGraph.getStart() == 1 && pCurentGraph.getFinish() == 1) {
            dataManger.getAuxDataManager().hideAuxiliaryGraph();
        }
        if (pCurentGraph){
            dataManger.recordRepaintScope(pCurentGraph);
        }
    }
}

LeftMoseUpPlotTool.correctLineEndPt = function (lineGraph, scaling) {
    if (lineGraph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type) {
        return 0;
    }
    let position = lineGraph.getPosCopy();
    let ptCount = position.posList.length;
    if (ptCount < 3) {
        return 0;
    }
    let tailPt = position.getTail();
    let next2TailPt = position.getNext2Tail();
    let dis = funcs.distancePoint(tailPt, next2TailPt, 1);
    const validLen = global.MinSelRectGap / scaling;
    if (dis > validLen) {
        return 0;
    }
    //挨着末端点的坐标点如果与末尾基本重合，则从坐标点队列中删掉该点
    position.posList.splice(ptCount - 2, 1);
    lineGraph.setPos(position);
    lineGraph.calScope(1);
}

export default { LeftMoseUpPlotTool }